package com.it.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.reggie.dao.SetmealDao;
import com.it.reggie.dao.SetmealDishDao;
import com.it.reggie.dto.SetmealDto;
import com.it.reggie.entity.Setmeal;
import com.it.reggie.entity.SetmealDish;
import com.it.reggie.service.ISetmealDishService;
import com.it.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:cyh
 * @data:2022/12/16 22:01
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements ISetmealService {
    @Autowired
    SetmealDao setmealDao;
    @Autowired
    SetmealDishDao setmealDishDao;
    @Autowired
    ISetmealDishService setmealDishService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveSetmealWith(SetmealDto setmealDto) {
        //1、保存套餐数据到 setmeal表中 ： SetmealMapper
        setmealDao.insert(setmealDto);
        //2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
        //2.1 获取当前套餐的菜品列表
        /*
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        //2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        for (SetmealDish setmealDish : setmealDishList) {
            //2.3 获取套餐id，并set到setmealDish中
            setmealDish.setSetmealId(setmealDto.getId());
            //2.4 保存套餐菜品数据 setmealDish
            setmealDishDao.insert(setmealDish);
        }*/



        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            // 保存菜品和套餐关联的信息，操作setMeal_dish,执行insert操作
            //setmealDishDao.insert(item);
            return item;
        }).collect(Collectors.toList());

        // 保存菜品和套餐关联的信息，操作setMeal_dish,执行insert操作
        //setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSetmeal(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1);
        wrapper.in(Setmeal::getId, ids);
        Integer count = setmealDao.selectCount(wrapper);
        if (count>0){
            return false;
        }
        setmealDao.deleteBatchIds(ids);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishDao.delete(queryWrapper);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmeal(SetmealDto setmealDto) {
        //1.更新setmeal表
        setmealDao.updateById(setmealDto);
        //2.更新套餐对应的菜品表
        //2.1 通过套餐id删除菜品表的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishDao.delete(wrapper);
        //2.2 将更新过后的菜品添加到setmealDish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishDao.insert(setmealDish);
        }
    }
}
